"""将对应的json数据转换成json字符串返回给前段，生成json文件"""
from flask import Flask, jsonify
import json

app = Flask(__name__)


@app.route('/')
def hello_world():
    return "hello world"


@app.route('/json')
def demo1():
    """json数据的转换"""
    dict = {
        "name": "james",
        "age": 33,
        "info": {
            "team": "laker"
        }
    }
    # dumps的说明"""Serialize ``obj`` to a JSON formatted ``str``.
    # 序列化：python对象转换成json字符串
    json_str = json.dumps(dict)

    # loads的说明："""Deserialize ``s`` (a ``str`` instance containing a JSON document) to a Python object.
    # 反序列化：json字符串转换成python对象（可以是list  dict等）
    my_dict = json.loads(json_str)
    print(my_dict)

    # jsonify的说明：1、将python对象转换成json字符串
    #               2、指明但会的数据类型：Content-Type:"application/json"
    #               3、将响应体数据包装成响应对象
    response = jsonify(dict)
    # 如：
    # <Response 71 bytes [200 OK]>
    # response.headers["Content-Type"] = "application/json"
    print(response)

    # return json_str
    return response


if __name__ == '__main__':
    app.run(debug=True)
